package com.huike.contract.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.common.utils.spring.SpringUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.domain.dto.TbContractDTO;
import com.huike.contract.service.TbContractService;
import com.huike.contract.mapper.TbContractMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 93238
* @description 针对表【tb_contract(合同)】的数据库操作Service实现
* @createDate 2023-10-12 06:42:55
*/
@Service
@RequiredArgsConstructor
public class TbContractServiceImpl extends ServiceImpl<TbContractMapper, TbContract>
    implements TbContractService{

    private final TbContractMapper tbContractMapper;

    private final TbBusinessMapper tbBusinessMapper;

    /**
     * 合同列表
     * @param tbContractDTO
     * @return
     */
    @Override
    public List<TbContractDTO> selectTbContract(TbContractDTO tbContractDTO) {
        if(!SecurityUtils.getAdmin().equals(SecurityUtils.getUserId())){
            //当前用户是admin不设置查询条件
            tbContractDTO.setCreateBy(SecurityUtils.getUsername());
        }
        List<TbContractDTO> list= tbContractMapper.selectTbContract(tbContractDTO);
        return list;
    }

    /**
     * 新增合同
     * @param tbContract
     * @return
     */
    @Override
    public int add(TbContract tbContract) {

        String businessKey=""+System.nanoTime();
        tbContract.setId(businessKey);
        tbContract.setCreateBy(SecurityUtils.getUsername());
        tbContract.setCreateTime(DateUtils.getNowDate());
        tbContract.setCreateBy(SecurityUtils.getUsername());
        tbContract.setCreateTime(DateUtils.getNowDate());
        tbContract.setDeptId(SecurityUtils.getDeptId());

        tbContract.setFileName(StringUtils.getRandom(32)+".pdf");

        tbContract.setStatus("4");//状态(待审核1，已通过2，已驳回3 全部完成4)

        return tbContractMapper.insertTbContract(tbContract);
    }

    /**
     * 商机转合同
     * @param id
     * @param tbContract
     * @return
     */
    @Override
    public int changeContract(Long id, TbContract tbContract) {
        //判断商机是否选择课程
        TbBusiness tbBusiness = tbBusinessMapper.selectById(id);
        if(tbBusiness.getCourseId()==null){
            throw new CustomException("商机里面没有选择课程，无法转换客户合同");
        }

        //tbBusiness.setStatus("7");
        //tbBusinessMapper.updateTbBusiness(tbBusiness);

        tbContract.setCreateTime(DateUtils.getNowDate());
        tbContract.setId(String.valueOf(id));
        BeanUtils.copyProperties(tbBusiness, tbContract);

        return baseMapper.insertTbContract(tbContract);
    }


}




